import time
from unittest import TestCase

import pymysql
import xlrd
from ddt import ddt, data, unpack
from selenium import webdriver
from selenium.webdriver.common.by import By

wb = xlrd.open_workbook(filename="Automation.xlsx")
st = wb.sheet_by_index(6)
rows = st.nrows
date = []
da = []
name=[]
con = pymysql.connect(user="root",password="root",database="finance",charset='utf8')
cursor = con.cursor()
sql='''select username from user'''
cursor.execute(sql)
name=cursor.fetchall()

for row in range(1, rows):
    date = st.row_values(row)
    username = str(date[0])
    password = str(int(date[1]))
    repassword=str(int(date[2]))
    da.append([username, password,repassword])
    @ddt
    class Testregister(TestCase):
        @data(*da)
        @unpack
        def testregister(self,username,password,repassword):

            driver = webdriver.Chrome()
            driver.get("http://localhost:90")
            driver.maximize_window()
            time.sleep(1)
            driver.find_element(By.XPATH, "/html/body/div/div/div/div[2]/a").click()
            time.sleep(1)
            driver.find_element(By.XPATH, "//input[@name='username']").send_keys(username)
            time.sleep(1)

            driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
            time.sleep(1)
            driver.find_element(By.XPATH, "//input[@id='repassword']").send_keys(repassword)
            time.sleep(1)

            if (username,) in name or password != repassword:
                try:
                    driver.find_element(By.XPATH, "//*[@id='login_btn']").click()
                except:
                    self.assertEqual(1, 1)
                driver.save_screenshot("Testregister" + username + "----" + password + ".jpg")
                time.sleep(1)
                self.assertEqual(1, 2)
            else:
                result = driver.find_element(By.XPATH, "//*[@id='login_btn']").text
                try:
                    driver.find_element(By.XPATH, "//*[@id='login_btn']").click()
                    time.sleep(1)
                    self.assertEqual(1,1)
                except:
                    driver.save_screenshot("Testregister" + username + "----" + password + ".jpg")
                    self.assertEqual(1,2)

            driver.quit()






















